package time

import (
	"fmt"
	"time"
)

func doWork(id int, timeout time.Duration, done chan bool) {
	select {
	case <-time.After(timeout):
		fmt.Printf("Task %d timed out.\n", id)
		return
	case <-done:
		fmt.Printf("Task %d completed.\n", id)
		return
	}
}

func Test() {
	done := make(chan bool)

	// 启动一个带有超时的goroutine
	go doWork(1, 2*time.Second, done)

	// 假设在这里执行了一些任务...
	time.Sleep(3 * time.Second) // 模拟任务执行

	// 通知goroutine完成，并退出
	done <- true

	// 等待goroutine真正退出
	time.Sleep(1 * time.Second)
}
